# coding:utf-8
from odoo import models, fields, api, _
from odoo.exceptions import Warning
import odoo.addons.decimal_precision as dp


class PaulCrmTeamExtend(models.Model):
    _name = 'crm.team'
    _inherit = 'crm.team'

    parent_id = fields.Many2one('crm.team', string=u'上级团队')


# class PaulSaleOrderExtend(models.Model):
#     _name = 'sale.order'
#     _inherit = 'sale.order'
#
#     section_leader_ids = fields.Many2many('res.users', 'sale_order_team_rel', 'order_id', 'user_id', string='Sales Team Leaders')
#     user_id = fields.Many2one('res.users', string='Salesperson', index=True, track_visibility='onchange', default=lambda self: self.env.user, copy=True)
#
#     def _calc_section(self, section_ids):
#         leads = []
#
#         for section_id in section_ids:
#             # add parent section
#             while True:
#                 if section_id.user_id is not None and section_id.user_id.id is not False:
#                     leads.append(section_id.user_id.id)
#                 if section_id.parent_id is not None and section_id.parent_id.id is not False:
#                     section_id = section_id.parent_id
#                     continue
#                 else:
#                     break
#         values = {'section_leader_ids': leads}
#         return values
#
#     @api.multi
#     def write(self, vals):
#         sales_person = self.user_id.id
#         if 'user_id' in vals:
#             sales_person = vals['user_id']
#         section_ids = self.env['crm.team'].search(
#             ['|', ('user_id', '=', sales_person), ('member_ids', 'in', [sales_person])])
#         values = self._calc_section(section_ids)
#         if len(values['section_leader_ids']) > 0:
#             vals['section_leader_ids'] = [(4, values['section_leader_ids'])]
#         else:
#             raise Warning(u'请确认销售员属于至少一个销售团队,并且此销售团队需要设定团队领导')
#         return super(PaulSaleOrderExtend, self).write(vals)
#
#     @api.model
#     def create(self, vals):
#         section_ids = self.env['crm.team'].search(
#             ['|', ('user_id', '=', self._uid), ('member_ids', 'in', [self._uid])])
#         values = self._calc_section(section_ids)
#         if len(values['section_leader_ids']) > 0:
#             vals['section_leader_ids'] = [(4, values['section_leader_ids'])]
#         else:
#             raise Warning(u'请确认销售员属于至少一个销售团队,并且此销售团队需要设定团队领导')
#         return super(PaulSaleOrderExtend, self).create(vals)


# class PaulProductCategoryExtend(models.Model):
#     _name = 'product.category'
#     _inherit = 'product.category'
#
#     user_id = fields.Many2one('res.users', string=u'销售员', index=True, track_visibility='onchange', default=lambda self: self.env.user)
#     section_leader_ids = fields.Many2many('res.users', 'product_category_team_rel', 'catg_id', 'user_id', string='Sales Team Leaders')
#
#     def _calc_section(self, section_ids):
#         leads = []
#
#         for section_id in section_ids:
#             # add parent section
#             while True:
#                 if section_id.user_id is not None and section_id.user_id.id is not False:
#                     leads.append(section_id.user_id.id)
#                 if section_id.parent_id is not None and section_id.parent_id.id is not False:
#                     section_id = section_id.parent_id
#                     continue
#                 else:
#                     break
#         values = {'section_leader_ids': leads}
#         return values
#
#     @api.multi
#     def write(self, vals):
#         sales_person = self.user_id.id
#         if 'user_id' in vals:
#             sales_person = vals['user_id']
#         section_ids = self.env['crm.team'].search(
#             ['|', ('user_id', '=', sales_person), ('member_ids', 'in', [sales_person])])
#         values = self._calc_section(section_ids)
#         if len(values['section_leader_ids']) > 0:
#             vals['section_leader_ids'] = [(4, values['section_leader_ids'])]
#         else:
#             raise Warning(u'请确认销售员属于至少一个销售团队,并且此销售团队需要设定团队领导')
#         return super(PaulProductCategoryExtend, self).write(vals)
#
#     @api.model
#     def create(self, vals):
#         section_ids = self.env['crm.team'].search(
#             ['|', ('user_id', '=', self._uid), ('member_ids', 'in', [self._uid])])
#         values = self._calc_section(section_ids)
#         if len(values['section_leader_ids']) > 0:
#             vals['section_leader_ids'] = [(4, values['section_leader_ids'])]
#         else:
#             raise Warning(u'请确认销售员属于至少一个销售团队,并且此销售团队需要设定团队领导')
#         return super(PaulProductCategoryExtend, self).create(vals)